package com.ftwj.controller;

import com.ftwj.entity.SysRole;
import com.ftwj.service.SysMenuListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ftwj.entity.SysMenu;

import java.util.List;

@RestController
@RequestMapping("/sysmenu")
public class SysMenuListController {
    @Autowired
    private SysMenuListService sysMenuListService;

    @GetMapping("/select")
    public List<SysMenu> getAllMenus() {
        return sysMenuListService.getAllMenus();
    }

    @GetMapping("/{parentId}")
    public List<SysMenu> getMenusByParentId(@PathVariable String parentId) {
        return sysMenuListService.getMenusByParentId(parentId);
    }

    @PostMapping("/hierarchy")
    public PageInfo<SysMenu> getMenuHierarchy(@RequestBody SysMenu sysMenu) {
        Integer pageNum = sysMenu.getPageNumber();
        Integer pageSize = sysMenu.getPageSize();
        // 初始化分页，设置当前页码和每页大小
        PageHelper.startPage(pageNum, pageSize);
        // 调用Service方法获取数据
        List<SysMenu> listMenus = sysMenuListService.getMenuHierarchy();
        // 将查询结果转换为PageInfo对象，包含了分页数据和额外的分页信息
        PageInfo<SysMenu> pageInfo = new PageInfo<>(listMenus);

        return pageInfo;
    }

    @GetMapping("/menus/name")
    public List<SysMenu> getMenuByName(@RequestParam String menuName) {
        return sysMenuListService.getMenuByName(menuName);
    }
}


